﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Wusong.Gold.DataService
{
    /// <summary>
    /// http://sj.zhijinwang.cn/goldusd.asp
    /// </summary>
    public class SJZJResource : IResourceBase
    {

        private string dollarMatch = "美元金.*";
        private string midPriceMatch = "中间价.*";
        private string oilPriceMatch = "石油价.*";
        private string dollarIndexMatch = "美指数.*";
        private string timeMatch = "时间.*";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="textValue">时间:2010-8-25 18:01:41,中间价:270.23,卖出价:269.83,买入价:270.63,美元价:1235.60,美元指数:83.13,石油指数:71.69</param>
        /// <returns></returns>
        public GoldMsg Formart(string textValue)
        {
            textValue = GetValue(textValue);
            double midPrice, salePrice, buyPrice, dollarPrice, dollarIndex, oilIndex;
            DateTime time;
            string[] nameValueKeys = textValue.Split(',');
            string timeStr = nameValueKeys[0];
            string[] timeArr = timeStr.Split(':');
            string dateAndHour = timeArr[1];
            string[] dateAndHourArr = dateAndHour.Split(' ');
            string dateStr = dateAndHourArr[0];

            string hour = dateAndHourArr[1];

            string[] dateArr = dateStr.Split('-');

            string minute = timeArr[2];

            string second = timeArr[3];

            midPrice = Convert.ToDouble(nameValueKeys[1].Split(':')[1]);
            salePrice = Convert.ToDouble(nameValueKeys[2].Split(':')[1]);
            buyPrice = Convert.ToDouble(nameValueKeys[3].Split(':')[1]);
            dollarPrice = Convert.ToDouble(nameValueKeys[4].Split(':')[1]);

            dollarIndex = Convert.ToDouble(nameValueKeys[5].Split(':')[1]);
            oilIndex = Convert.ToDouble(nameValueKeys[6].Split(':')[1]);
            time = new DateTime(int.Parse(dateArr[0]), int.Parse(dateArr[1]), int.Parse(dateArr[2]), int.Parse(hour), int.Parse(minute), int.Parse(second));

            return new GoldMsg()
            {
                MsgTime = time,
                MidPrice = midPrice,
                SalePrice = salePrice,
                BuyPrice = buyPrice,
                DollarPrice = dollarPrice,
                DollarIndex = dollarIndex,
                OilIndex = oilIndex
            };
        }

        private string GetValue(string html)
        {
           
            Regex reg = null;
            Match match = null;

            reg = new Regex(midPriceMatch, RegexOptions.IgnoreCase);
            match = reg.Match(html);
            string midPrice = TrimPrice("中间价", match.ToString());

            reg = new Regex(dollarMatch, RegexOptions.IgnoreCase);
            match = reg.Match(html);
            string dollarPrice = TrimPrice("美元金", match.ToString());

            reg = new Regex(dollarIndexMatch, RegexOptions.IgnoreCase);
            match = reg.Match(html);
            string dollorIndex = TrimPrice("美指数", match.ToString());

            reg = new Regex(oilPriceMatch, RegexOptions.IgnoreCase);
            match = reg.Match(html);
            string oilPrice = TrimPrice("石油价", match.ToString());

            reg = new Regex(timeMatch, RegexOptions.IgnoreCase);
            match = reg.Match(html);
            string tiemStr = TrimTime(match.ToString());

            double midPriceDb = Convert.ToDouble(midPrice);
            string result = string.Format("时间:{0},中间价:{1},卖出价:{2},买入价:{3},美元价:{4},美元指数:{5},石油指数:{6}",
                tiemStr, midPrice, midPriceDb - 0.4, midPriceDb + 0.4, dollarPrice, dollorIndex, oilPrice) + "\r\n";

            return result;
        }

        private string TrimPrice(string nodeName, string price)
        {
            return price.Replace(nodeName + "</font></a>:<font color=\"#FF0000\">", string.Empty).Replace("</font><br>", string.Empty).Trim();
        }

        private string TrimTime(string time)
        {
            return DateTime.Now.ToShortDateString() + " " + time.Replace("时间:<font color=\"#FF0000\">", string.Empty).Replace("</font></font></td>", string.Empty).Trim();
        }

        public const string _dataUri = "http://sj.zhijinwang.cn/goldusd.asp";
        public string DataUri
        {
            get
            {
                return _dataUri;
            }

        }
    }


}
